package strategy;

import java.util.HashMap;
import java.util.HashSet;

public class OverlapStrategy extends Strategy {

	//google=0.3653
	//baidu=0.2908
	//youdao=0.2648
	
	//this=33.23
	@Override
	public String selectAnswer(HashMap<String, String> results) {
		
		String yd = results.get("youdao");
		String bd = results.get("baidu");
		String gg = results.get("google");
		//System.out.println("================");
		//System.out.println("yd = " + yd);
		//System.out.println("bd = " + bd);
		//System.out.println("gg = " + gg);
		
		double ydScore = (getOverlapScore(yd, bd) + getOverlapScore(yd, gg)) / 2;
		double bdScore = (getOverlapScore(bd, yd) + getOverlapScore(bd, gg)) / 2;
		double ggScore = (getOverlapScore(gg, yd) + getOverlapScore(gg, bd)) / 2;
		//System.out.println("******" + "ydScore = " + ydScore + "\t" + "bdScore = " + bdScore + "\t" + "ggScore = " + ggScore);
		
		
		if( ydScore > bdScore && ydScore > ggScore) {
			System.out.println("yd");
			return yd;
		} else if( bdScore > ydScore && bdScore > ggScore) {
			System.out.println("bd");
			return bd;
		} else {
			System.out.println("gg");
			return gg;
		}
	}
	
	double getOverlapScore(String s1, String s2) {
		HashSet<String> set1 = new HashSet<String>();
		String[] splits = s1.split(" +");
		for( int i = 0; i < splits.length; i++) {
			String token = splits[i].toLowerCase().replaceAll("^[^a-z0-9]*", "").replaceAll("[^a-z0-9]*$", "");
			if(token.length() > 0) {
				//System.out.println("token = " + token);
				set1.add(token);
			}
		}
		HashSet<String> set2 = new HashSet<String>();
		splits = s2.split(" +");
		for( int i = 0; i < splits.length; i++) {
			String token = splits[i].toLowerCase().replaceAll("^[^a-z0-9]*", "").replaceAll("[^a-z0-9]*$", "");
			if( token.length() > 0) {
				//System.out.println("token = " + token);
				set2.add(token);
			}
		}
		int inter = 0;
		for(String token1 : set1) {
			if( set2.contains(token1)) {
				inter++;
			}
		}
		return (0.0 + inter) / (set1.size() + set2.size() - inter);
	}

	public static void main(String[] args) {
		//new OverlapStrategy().evaluate();
	}

	@Override
	double getTranslationScore(TranslatinData f) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}


}
